package org.dromara.bzIns.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.bzIns.domain.vo.InsInsureVo;
import org.dromara.bzIns.domain.bo.InsInsureBo;
import org.dromara.bzIns.service.IInsInsureService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 保单记录
 *
 * @author bz_huaYal9146
 * @date 2025-03-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/bzIns/insInsure")
public class InsInsureController extends BaseController {

    private final IInsInsureService insInsureService;


    /**
     * 查询保单记录列表
     */
    @SaCheckPermission("bzIns:insInsure:list")
    @GetMapping("/list")
    public TableDataInfo<InsInsureVo> list(InsInsureBo bo, PageQuery pageQuery) {
        return insInsureService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出保单记录列表
     */
    @SaCheckPermission("bzIns:insInsure:export")
    @Log(title = "保单记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(InsInsureBo bo, HttpServletResponse response) {
        List<InsInsureVo> list = insInsureService.queryList(bo);
        ExcelUtil.exportExcel(list, "保单记录", InsInsureVo.class, response);
    }

    /**
     * 获取保单记录详细信息
     *
     * @param insureId 主键
     */
    @SaCheckPermission("bzIns:insInsure:query")
    @GetMapping("/{insureId}")
    public R<InsInsureVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long insureId) {
        return R.ok(insInsureService.queryById(insureId));
    }

    /**
     * 新增保单记录
     */
    @SaCheckPermission("bzIns:insInsure:add")
    @Log(title = "保单记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody InsInsureBo bo) {
        return toAjax(insInsureService.insertByBo(bo));
    }

    /**
     * 修改保单记录
     */
    @SaCheckPermission("bzIns:insInsure:edit")
    @Log(title = "保单记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody InsInsureBo bo) {
        return toAjax(insInsureService.updateByBo(bo));
    }

    /**
     * 删除保单记录
     *
     * @param insureIds 主键串
     */
    @SaCheckPermission("bzIns:insInsure:remove")
    @Log(title = "保单记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{insureIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] insureIds) {
        return toAjax(insInsureService.deleteWithValidByIds(List.of(insureIds), true));
    }
}
